//
//  RootView.m
//  NBNews
//
//  Created by Ruan on 14-10-8.
//  Copyright (c) 2014年 Hero. All rights reserved.
//

#import "RootView.h"
#import "LPSButton.h"
#import "UIButton+NavButton.h"


#define kScreen [UIScreen mainScreen].bounds
#define kCustomColor(r,g,b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define kRootViewWRatio 0.75
#define kRootViewAlpha 0.95
#define kButtonCount 6
#define kButtonToTopH 62
#define kNewsImageCount 19
#define subscribeImageCount 17
#define kPictureImageCount 17
#define kVideoImageCount 17
#define kCommentImageCount 19
#define kMusicImageCount 19



@implementation RootView

-(void)dealloc
{
    
    [_newsButton release];
    [_subscribeButton release];
    [_pictureButton release];
    [_VideoButton release];
    [_commentButton release];
    [_musicButton release];
       
    [_newsImageView release];
    [_subscribeImageView release];
    [_pictureImageView release];
    [_videoImageView release];
    [_commentImageView release];
    [_musicImageView release];

    [_newsImageArray release];
    [_subscribeImageArray release];
    [_pictureImageArray release];
    [_videoImageArray release];
    [_commentImageArray release];
    [_musicImageArray release];
    
    [_newscircleView release];
    [_subscribecircleView release];
    [_picturecircleView release];
    [_videocircleeView release];
    [_commentcircleView release];
    [_musiccircleView release];    

    [super dealloc];
}




- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = kCustomColor(19, 19, 19);
        
        
        CGFloat rootViewY = 0;
        CGFloat rootViewW = kScreen.size.width * kRootViewWRatio;
        CGFloat rootViewH = kScreen.size.height;
        CGFloat rootViewX = -rootViewW ;
        
        self.frame = CGRectMake(rootViewX, rootViewY, rootViewW, rootViewH);
        self.alpha = kRootViewAlpha;
        
        
        UIButton *newsButton = [UIButton addRootViewButtonWithNormalTitilString:@"新闻" ];
        [newsButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        self.newsButton = newsButton;
        
        UIButton *subscribeButton = [UIButton addRootViewButtonWithNormalTitilString:@"清除缓存"];
        [subscribeButton setTitleColor:[UIColor colorWithRed:230/255.0 green:46/255.0 blue:37/255.0 alpha:1.0] forState:UIControlStateSelected];
        self.subscribeButton = subscribeButton;
         
        UIButton *pictureButton = [UIButton addRootViewButtonWithNormalTitilString:@"图片"];
        [pictureButton setTitleColor:[UIColor colorWithRed:71/255.0 green:145/255.0 blue:230/255.0 alpha:1.0] forState:UIControlStateSelected];
        self.pictureButton = pictureButton;
        

        UIButton *videoButton = [UIButton addRootViewButtonWithNormalTitilString:@"视频"];
        [videoButton setTitleColor:[UIColor colorWithRed:68/255.0 green:170/255.0 blue:51/255.0 alpha:1.0] forState:UIControlStateSelected];
        self.VideoButton = videoButton;
        
        UIButton *commentButton = [UIButton addRootViewButtonWithNormalTitilString:@"收藏"];
        [commentButton setTitleColor:[UIColor colorWithRed:227/255.0 green:120/255.0 blue:38/255.0 alpha:1.0] forState:UIControlStateSelected];
        self.commentButton = commentButton;
        
        
        UIButton *musicButton = [UIButton addRootViewButtonWithNormalTitilString:@"LPSNews"];
        [musicButton setTitleColor:[UIColor colorWithRed:65/255.0 green:186/255.0 blue:250/255.0 alpha:1.0] forState:UIControlStateNormal];
        musicButton.titleLabel.font = [UIFont systemFontOfSize:26];
        self.musicButton = musicButton;
        musicButton.enabled = NO;
        
        
        
        NSMutableArray *newsImageArray = [[NSMutableArray alloc] init];
        for (int i = 0; i < kNewsImageCount; i++) {
            NSString *newsImagePath = [NSString stringWithFormat:@"100%02d",i + 1];
            UIImage *newsImage = [UIImage imageNamed:newsImagePath];
            [newsImageArray addObject:newsImage];
        }
        
        NSMutableArray *pictureImageArray = [[NSMutableArray alloc] init];
        for (int i = 0; i < kPictureImageCount; i++) {
            NSString *pictureImagePath = [NSString stringWithFormat:@"200%02d",i + 1];
            UIImage *pictureImage = [UIImage imageNamed:pictureImagePath];
            [pictureImageArray addObject:pictureImage];
        }
        
        NSMutableArray *videoImageArray = [[NSMutableArray alloc] init];
        for (int i = 0; i < kVideoImageCount; i++) {
            NSString *videoImagePath = [NSString stringWithFormat:@"400%02d",i + 1];
            UIImage *videoImage = [UIImage imageNamed:videoImagePath];
            [videoImageArray addObject:videoImage];
        }
        
        
        NSMutableArray *commentImageArray = [[NSMutableArray alloc] init];
        for (int i = 0; i < kCommentImageCount; i++) {
            NSString *commentImagePath = [NSString stringWithFormat:@"500%02d",i + 1];
            UIImage *commentImage = [UIImage imageNamed:commentImagePath];
            [commentImageArray addObject:commentImage];
        }
        
        NSMutableArray *subscribeImageArray = [[NSMutableArray alloc] init];
        for (int i = 0; i < subscribeImageCount; i++) {
            NSString *subscribeImagePath = [NSString stringWithFormat:@"300%02d",i + 1];
            UIImage *subscribeImage = [UIImage imageNamed:subscribeImagePath];
            [subscribeImageArray addObject:subscribeImage];
        }
        
        
        
//        NSMutableArray *musicImageArray = [[NSMutableArray alloc] init];
//        for (int i = 0; i < kMusicImageCount; i++) {
//            NSString *musicImagePath = [NSString stringWithFormat:@"V400%02d",i + 1];
//            UIImage *musicImage = [UIImage imageNamed:musicImagePath];
//            [musicImageArray addObject:musicImage];
//        }
        
        self.newsImageArray  = newsImageArray;
        self.subscribeImageArray = subscribeImageArray;
        self.pictureImageArray = pictureImageArray;
        self.videoImageArray= videoImageArray;
        self.commentImageArray = commentImageArray;
        //self.musicImageArray = musicImageArray;
        
        UIImageView *newsImageView = [[[UIImageView alloc] init] autorelease];
        UIImageView *subscribeImageView = [[[UIImageView alloc] init] autorelease];
        UIImageView *pictureImageView = [[[UIImageView alloc] init] autorelease];
        UIImageView *videoImageView = [[[UIImageView alloc] init] autorelease];
        UIImageView *commentImageView = [[[UIImageView alloc] init] autorelease];
        //UIImageView *musicImageView = [[[UIImageView alloc] init] autorelease];
        
        newsImageView.image = newsImageArray.lastObject;
        subscribeImageView.image = subscribeImageArray.lastObject;
        pictureImageView.image = pictureImageArray.lastObject;
        videoImageView.image = videoImageArray.lastObject;
        commentImageView.image = commentImageArray.lastObject;
       // musicImageView.image = musicImageArray.lastObject;
        
        newsImageView.alpha = 0;
        subscribeImageView.alpha = 0;
        pictureImageView.alpha = 0;
        videoImageView.alpha = 0;
        commentImageView.alpha = 0;
       // musicImageView.alpha = 0;

 
        self.newsImageView = newsImageView;
        self.subscribeImageView = subscribeImageView;
        self.pictureImageView = pictureImageView;
        self.videoImageView = videoImageView;
        self.commentImageView = commentImageView;
       // self.musicImageView = musicImageView;

        UIImage *animationImage = newsImageArray[18];
        CGSize animationImageSize = animationImage.size;

         newsImageView.frame = (CGRect){CGPointZero,animationImageSize};
         subscribeImageView.frame = (CGRect){CGPointZero,animationImageSize};
         pictureImageView.frame = (CGRect){CGPointZero,animationImageSize};
         videoImageView.frame = (CGRect){CGPointZero,animationImageSize};
         commentImageView.frame = (CGRect){CGPointZero,animationImageSize};
        // musicImageView.frame = (CGRect){CGPointZero,animationImageSize};
        
        
        UIImageView *newscircleView = [[[UIImageView alloc] init] autorelease];
        UIImageView *subscribecircleView = [[[UIImageView alloc] init] autorelease];
        UIImageView *picturecircleView = [[[UIImageView alloc] init] autorelease];
        UIImageView *videocircleeView = [[[UIImageView alloc] init] autorelease];
        UIImageView *commentcircleView = [[[UIImageView alloc] init] autorelease];
       // UIImageView *musiccircleView = [[[UIImageView alloc] init] autorelease];
        
        newscircleView.image = [UIImage imageNamed:@"newsbar_circle"];
        subscribecircleView.image = [UIImage imageNamed:@"subscribe_circle"];
        picturecircleView.image = [UIImage imageNamed:@"picture_circle"];
        videocircleeView.image = [UIImage imageNamed:@"video_circle"];
        commentcircleView.image = [UIImage imageNamed:@"comment_circle"];
       // musiccircleView.image = [UIImage imageNamed:@"music_circle"];
        

        self.newscircleView = newscircleView;
        self.subscribecircleView = subscribecircleView;
        self.picturecircleView = picturecircleView;
        self.videocircleeView = videocircleeView;
        self.commentcircleView = commentcircleView;
        //self.musiccircleView = musiccircleView;
        
        
        CGSize  circleSize = [UIImage imageNamed:@"music_circle"].size;
        CGFloat circleSizeW = circleSize.width * 0.7;
        CGFloat circleSizeH = circleSize.height * 0.7;
        circleSize = CGSizeMake(circleSizeW, circleSizeH);
        self.circleSize = circleSize;
        
        newscircleView.frame = (CGRect){CGPointZero,circleSize};
        subscribecircleView.frame = (CGRect){CGPointZero,circleSize};
        picturecircleView.frame = (CGRect){CGPointZero,circleSize};
        videocircleeView.frame = (CGRect){CGPointZero,circleSize};
        commentcircleView.frame = (CGRect){CGPointZero,circleSize};
       // musiccircleView.frame = (CGRect){CGPointZero,circleSize};
        
        [self addSubview:newsButton];
        [self addSubview:pictureButton];
        [self addSubview:videoButton];
        [self addSubview:commentButton];
        [self addSubview:subscribeButton];
        [self addSubview:musicButton];
        
        [newsButton addSubview:newscircleView];
        [subscribeButton addSubview:subscribecircleView];
        [pictureButton addSubview:picturecircleView];
        [videoButton addSubview:videocircleeView];
        [commentButton addSubview:commentcircleView];
       // [musicButton addSubview:musiccircleView];
        
    
        [newsButton addSubview:newsImageView];
        [subscribeButton addSubview:subscribeImageView];
        [pictureButton addSubview:pictureImageView];
        [videoButton addSubview:videoImageView];
        [commentButton addSubview:commentImageView];
       // [musicButton addSubview:musicImageView];

        
        [newsImageArray release];
        [subscribeImageArray release];
        [pictureImageArray release];
        [videoImageArray release];
        [commentImageArray release];
       // [musicImageArray release];
        
       
    
    }
    return self;
}


- (void)playRootViewButtonImageAnimation
{
    self.newsImageView.alpha = 1;
    self.subscribeImageView.alpha = 1;
    self.pictureImageView.alpha = 1;
    self.videoImageView.alpha = 1;
    self.commentImageView.alpha = 1;
    //self. musicImageView.alpha = 1;

    self.newsImageView.animationImages = self.newsImageArray;
    self.subscribeImageView.animationImages = self.subscribeImageArray;
    self.pictureImageView.animationImages = self.pictureImageArray;
    self.videoImageView.animationImages = self.videoImageArray;
    self.commentImageView.animationImages = self.commentImageArray;
    //self.musicImageView.animationImages = self.musicImageArray;
 
    self.newsImageView.animationDuration = 0.8;
    self.subscribeImageView.animationDuration = 0.8;
    self.pictureImageView.animationDuration = 0.8;
    self.videoImageView.animationDuration = 0.8;
    self.commentImageView.animationDuration = 0.8;
    //self.musicImageView.animationDuration = 0.8;
    
    self.newsImageView.animationRepeatCount = 1;
    self.subscribeImageView.animationRepeatCount = 1;
    self.pictureImageView.animationRepeatCount = 1;
    self.videoImageView.animationRepeatCount = 1;
    self.commentImageView.animationRepeatCount = 1;
  //  self.musicImageView.animationRepeatCount = 1;
    
    [self.newsImageView startAnimating];
    [self.subscribeImageView startAnimating];
    [self.pictureImageView startAnimating];
    [self.videoImageView startAnimating];
    [self.commentImageView startAnimating];
    //[self.musicImageView startAnimating];
    
    [self animationWithImageView:self.newscircleView];
    [self animationWithImageView:self.subscribecircleView];
    [self animationWithImageView:self.picturecircleView];
    [self animationWithImageView:self.videocircleeView];
    [self animationWithImageView:self.commentcircleView];
    //[self animationWithImageView:self.musiccircleView];
    
    [NSTimer scheduledTimerWithTimeInterval:0.79 target:self selector:@selector(dispalyRootViewImage) userInfo:nil repeats:NO];
    

}

- (void)dispalyRootViewImage
{
   
    
    [self.newsImageView stopAnimating];
    [self.subscribeImageView stopAnimating];
    [self.pictureImageView stopAnimating];
    [self.videoImageView stopAnimating];
    [self.commentImageView stopAnimating];
    //[self.musicImageView stopAnimating];

}

- (void)notDisplayRootViewButtonAnimation
{

    
    self.newsImageView.alpha = 0;
    self.subscribeImageView.alpha = 0;
    self.pictureImageView.alpha = 0;
    self.videoImageView.alpha = 0;
    self.commentImageView.alpha = 0;
   // self.musicImageView.alpha = 0;
    
    self.newscircleView.alpha = 0;;
    self.subscribecircleView.alpha = 0;
    self.picturecircleView.alpha = 0;
    self.videocircleeView.alpha = 0;
    self.commentcircleView.alpha = 0;
   // self.musiccircleView.alpha = 0;
    
}



- (void)layoutSubviews
{
    CGFloat btnX = 0;
    CGFloat btnY = 0;
    CGFloat btnW = kScreen.size.width * kRootViewWRatio;
    CGFloat btnH = (kScreen.size.height - kButtonToTopH * 2) * 1.0 / kButtonCount;
    for (int i = 0; i < kButtonCount; i++) {
        
        btnY = kButtonToTopH + btnH * i;
        UIButton *btn = self.subviews[i];
        btn.tag = i;
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
       
       
        
        if (i != kButtonCount -1)
        {
            
        btn.titleEdgeInsets = UIEdgeInsetsMake(0,btnW * 0.47, 0, 0);
        UIImageView *btnImageView = btn.subviews[2];

        btnImageView.frame = CGRectMake(btnW *0.27, 0, btnImageView.frame.size.width, btnImageView.frame.size.height);
        CGPoint btnImageViewCenter = btnImageView.center;
        btnImageViewCenter.y = btnH * 0.5 - 0.5;
        btnImageView.center = btnImageViewCenter;
        
        UIImageView *btnCircleView = btn.subviews[1];
        btnCircleView.alpha = 0;
        btnCircleView.frame = CGRectMake(0, 0, btnCircleView.frame.size.width, btnCircleView.frame.size.height);
        btnCircleView.center = btnImageView.center;
        }else
        {
            btn.titleEdgeInsets = UIEdgeInsetsMake(0,btnW * 0.35, 0, 0);
        }
        
        
    }
}

- (void)animationWithImageView:(UIImageView *)imageView
{
    CGPoint imageViewCenter = imageView.center;
    UIButton *button = (UIButton *)[imageView superview];
    
    if (button.selected) {
        
        imageView.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y, 40, 40);
        imageView.center = imageViewCenter;
    }else
    {
        imageView.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y, 28, 28);
        imageView.center = imageViewCenter;
    }

    imageView.alpha = 1;
    CGRect newscircleViewFrame = imageView.frame;
    
    [UIView animateWithDuration:0.01 animations:^{
        imageView.transform = CGAffineTransformScale(imageView.transform, 0.1, 0.1);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.4 animations:^{
            imageView.transform = CGAffineTransformScale(imageView.transform, 12, 12);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.12 animations:^{
                imageView.transform = CGAffineTransformScale(imageView.transform, 0.7, 0.7);
            } completion:^(BOOL finished) {
                [UIView animateWithDuration:0.12 animations:^{
                    
                    imageView.frame = newscircleViewFrame;
                }];
                
            }];
        }];
    }];
}



@end
